package com.tongfusoft.web.core.action;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;

import com.tongfusoft.exception.ServiceException;

public class UpLoadFileAction extends BaseAction {
	private File file_upload;
	/**
	 * 上传文件名
	 */
	private String file_uploadFileName;
	/**
	 * 临时保存路径
	 */
	private String savePath;
	/**
	 * 文件扩展名
	 */
	private String extName;
	/**
	 * 文件保存名称
	 */
	private String newFileName;
	/**
	 * 执行结果
	 */
	protected boolean result;
	
	/**
	 * 返回的提示信息
	 */
	protected String message;
	public String upLoad() {
		
		Map<String,String> res = new HashMap();
		try {
			savePath = UpLoadFileAction.class.getClassLoader().getResource("")
					.getPath();
			savePath = savePath + "../../uploads/tmp/";
			File f1 = new File(savePath);  
		    if (!f1.exists()) {  
		          f1.mkdirs();  
		      }
			extName = getFile_uploadFileName().substring(
					getFile_uploadFileName().lastIndexOf("."));
			newFileName = UUID.randomUUID() + extName;
			file_upload.renameTo(new File(savePath + newFileName));
			result = true;
			message = super.getText("common.prompt.success");
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
			if(e instanceof ServiceException && StringUtils.isNotEmpty(((ServiceException)e).getErrMsgCode())){
				message = super.getText(((ServiceException)e).getErrMsgCode());
			}else{
				message = super.getText("common.prompt.fail");
			}
		}
		return SUCCESS;
	}

	public File getFile_upload() {
		return file_upload;
	}

	public void setFile_upload(File file_upload) {
		this.file_upload = file_upload;
	}

	public String getFile_uploadFileName() {
		return file_uploadFileName;
	}

	public void setFile_uploadFileName(String file_uploadFileName) {
		this.file_uploadFileName = file_uploadFileName;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public String getExtName() {
		return extName;
	}

	public void setExtName(String extName) {
		this.extName = extName;
	}

	public String getNewFileName() {
		return newFileName;
	}

	public void setNewFileName(String newFileName) {
		this.newFileName = newFileName;
	}

	public boolean isResult() {
		return result;
	}

	public void setResult(boolean result) {
		this.result = result;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}
